<template>
	<view class="template-login">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back'>
				<text class='icon tn-icon-left' @click="goBack"></text>
				<text class='icon tn-icon-home-capsule-fill' @click="goHome"></text>
			</view>
		</tn-nav-bar>

		<view class="login">
			<!-- 顶部背景图片-->
			<view class="login__bg login__bg--top">
				<image class="bg" src="http://res.sunlee.icu/2023-07/20/fa5d778890ba437dbe5896f7852dca55.gif"
					mode="aspectFill">
				</image>
			</view>

			<view class="login__wrapper">

				<!-- 输入框内容-->
				<view class="login__info tn-flex tn-flex-direction-column tn-flex-col-center tn-flex-row-center">
					<!-- 登录 -->
					<view class="tn-flex">
						<view class=" login__mode__item_l tn-text-lg tn-color-white">
							找回密码
						</view>
					</view>

					<view class="tn-flex tn-width-full">
						<view class="tn-margin-top-xl tn-width-full">
							<tn-steps @click="clickStep" :current="current" mode="icon" :list="stepList"
								activeColor="#000000" inActiveColor="#FFFFFF"></tn-steps>
						</view>

					</view>
					<!-- 注册 -->
					<block>
						<view v-if="current === 1"
							class="login__info__item__input tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
							<view class="login__info__item__input__left-icon">
								<view class="tn-icon-email"></view>
							</view>
							<view class="login__info__item__input__content">
								<input v-model="registerEmailParam.email" maxlength="64"
									placeholder-class="input-placeholder" placeholder="请输入需要找回的邮箱" />
							</view>
						</view>

						<view v-else-if="current === 2"
							class="login__info__item__input tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
							<view class="login__info__item__input__left-icon">
								<view class="tn-icon-code"></view>
							</view>

							<view
								class="login__info__item__input__content login__info__item__input__content--verify-code">
								<input v-model="registerEmailParam.captcha" type="number"
									placeholder-class="input-placeholder" placeholder="请输入验证码" maxlength="6" />
							</view>

							<view class="login__info__item__input__right-verify-code" @tap.stop="getEmailCode">
								<tn-button backgroundColor="#01BEFF" fontColor="#FFFFFF" size="sm" padding="5rpx 10rpx"
									width="100%" shape="round">{{ tips }}</tn-button>
							</view>
						</view>

						<block v-else-if="current === 3">
							<view
								class="login__info__item__input tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
								<view class="login__info__item__input__left-icon">
									<view class="tn-icon-lock"></view>
								</view>
								<view class="login__info__item__input__content">
									<input v-model="registerEmailParam.password" maxlength="20"
										:password="!showPassword" placeholder-class="input-placeholder"
										placeholder="请输入登录密码" />
								</view>
								<view class="login__info__item__input__right-icon"
									@click="showPassword = !showPassword">
									<view :class="[showPassword ? 'tn-icon-eye' : 'tn-icon-eye-hide']"></view>
								</view>
							</view>

							<view
								class="login__info__item__input tn-flex tn-flex-direction-row tn-flex-nowrap tn-flex-col-center tn-flex-row-left">
								<view class="login__info__item__input__left-icon">
									<view class="tn-icon-lock"></view>
								</view>
								<view class="login__info__item__input__content">
									<input v-model="againPassword" maxlength="20" :password="!showPassword"
										placeholder-class="input-placeholder" placeholder="请再次输入登录密码" />
								</view>
								<view class="login__info__item__input__right-icon"
									@click="showPassword = !showPassword">
									<view :class="[showPassword ? 'tn-icon-eye' : 'tn-icon-eye-hide']"></view>
								</view>
							</view>
						</block>
					</block>


					<view @click="clickNext" class=" login__info__item__button tn-cool-bg-color-7--reverse"
						hover-class="tn-hover" :hover-stay-time="150">{{current===4?'去登录':'下一步'}}</view>

					<view v-if="current===1" class="login__info__item__tips">忘记邮箱，可联系作者</view>
					<view v-else-if="current===2" class="login__info__item__tips">遇到邮件延迟，可点击重新获取</view>
					<view v-else-if="current===3" class="login__info__item__tips">密码长度需在6-20位之间哦</view>
					<view v-else-if="current===4" class="login__info__item__tips">若完成更改，快去登录试试吧</view>
				</view>

			</view>

		</view>


		<!-- 验证码倒计时 -->
		<tn-verification-code ref="code" uniqueKey="login-demo-3" :seconds="60" @change="codeChange" />
	</view>
</template>

<script>
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js';
	import {
		sendResetPasswordEmailCaptcha,
		resetPasswordByEmail
	} from '@/api/auth.js';

	import validator from '@/utils/validateUtil.js';
	import ws from '@/api/websocket.js'

	import {
		uploadAvatar
	} from '@/utils/uploadUtil.js'
	export default {
		name: 'resetPassword',
		mixins: [template_page_mixin],
		data() {
			return {
				current: 1,
				stepList: [{
						name: '邮箱',
						icon: 'email',
						selectIcon: 'success-circle-fill'
					},
					{
						name: '验证码',
						icon: 'code',
						selectIcon: 'success-circle-fill'
					},
					{
						name: '密码',
						icon: 'lock',
						selectIcon: 'success-circle-fill'
					},
					{
						name: '完成',
						icon: 'success-circle',
						selectIcon: 'success-circle-fill'
					}
				],
				// 是否显示密码
				showPassword: false,
				// 倒计时提示文字
				tips: '获取验证码',
				registerEmailParam: {
					code: null,
					email: null,
					captcha: null,
					password: null
				},
				againPassword: null
			}
		},
		watch: {},
		onLoad() {
			this.registerEmailParam.code = Date.now().toString()
		},
		methods: {
			clickStep(e) {
				this.current = e.index
			},
			async clickNext() {
				// 获取验证码
				if (this.current === 1) {
					if (await this.getEmailCode()) {
						this.current++
					}
					return
				}

				// 输入验证码，或重发验证码
				if (this.current === 2) {
					if (this.checkCaptcha()) {
						this.current++
					}
					return
				}

				// 发送更改请求
				if (this.current === 3) {
					if (await this.submitResetPasswordByEmailData()) {
						this.current++
						// 清空记录
						this.registerEmailParam = null
					}
					return
				}

				// 完成，进行跳转
				if (this.current === 4) {
					// 关闭当前页，失败则直接调到登录页
					uni.navigateBack({
						fail() {
							uni.navigateBack({
								url: "/pageA/login/login"
							})
						}
					})
					return
				}
			},
			// 校验邮箱
			checkEmail() {
				return validator.email(this.registerEmailParam.email)
			},
			// 校验密码
			checkPassword() {
				if (this.againPassword !== this.registerEmailParam.password) {
					this.$t.message.toast('两次密码不一致')
					return false;
				}
				return validator.password(this.registerEmailParam.password)
			},
			// 校验验证码
			checkCaptcha() {
				return validator.captcha(this.registerEmailParam.captcha)
			},
			// 获取验证码
			async getEmailCode() {
				let flag = false;
				if (this.$refs.code.canGetCode) {
					if (!this.checkEmail()) {
						return flag
					}
					this.$t.message.loading('正在获取验证码')
					await sendResetPasswordEmailCaptcha({
						email: this.registerEmailParam.email,
						code: this.registerEmailParam.code
					}).then(res => {
						flag = true
						this.$t.message.closeLoading()
						this.$t.message.toast('验证码已经发送')
						this.$refs.code.start()
					})
				} else {
					this.$t.message.toast(this.$refs.code.secNum + '秒后再重试')
				}
				return flag;
			},
			// 发起重置密码请求
			async submitResetPasswordByEmailData() {
				let flag = false
				if (this.checkEmail() && this.checkCaptcha() && this.checkPassword()) {
					await resetPasswordByEmail(this.registerEmailParam).then(res => {
						flag = true
						this.$t.message.toast('密码重置成功！')
					})
				}
				return flag
			},
			// 获取验证码倒计时被修改
			codeChange(event) {
				this.tips = event
			},

		}
	}
</script>

<style lang="scss" scoped>
	/* 悬浮 */
	.login-sussuspension {
		animation: suspension 3s ease-in-out infinite;
	}

	@keyframes suspension {

		0%,
		100% {
			transform: translate(0, 0);
		}

		50% {
			transform: translate(0rem, 1rem);
		}
	}

	.login {
		position: relative;
		height: 100%;
		z-index: 1;

		/* 背景图片 start */
		&__bg {
			z-index: -1;
			position: fixed;

			&--top {
				top: 0;
				left: 0;
				right: 0;
				width: 100%;

				.bg {
					width: 100vw;
					height: 100vh;
					will-change: transform;
				}
			}

			/* &--bottom {
        bottom: -10rpx;
        left: 0;
        right: 0;
        width: 100%;
        margin-bottom: env(safe-area-inset-bottom);
        
        image {
          width: 750rpx;
          will-change: transform;
        }
      } */
		}

		/* 背景图片 end */

		/* 内容 start */
		&__wrapper {
			margin-top: 403rpx;
			width: 100%;
		}

		/* 切换 start */
		&__mode {
			position: relative;
			margin: 0 auto;
			width: 476rpx;
			height: 77rpx;
			background-color: rgba(255, 255, 255, 0.6);
			box-shadow: 0rpx 10rpx 50rpx 0rpx rgba(0, 3, 72, 0.1);
			border-radius: 39rpx;

			&__item {
				height: 77rpx;
				width: 100%;
				line-height: 77rpx;
				text-align: center;
				font-size: 31rpx;
				color: #FFFFFF;
				letter-spacing: 1em;
				text-indent: 1em;
				z-index: 2;
				transition: all 0.4s;

				&--active {
					font-weight: bold;
					color: #FFFFFF;
				}
			}

			&__item_l {
				width: 100%;
				text-align: center;
				font-size: 31rpx;
				color: #FFFFFF;
				letter-spacing: 0.3em;
				transition: all 0.4s;
			}

			&__slider {
				position: absolute;
				height: inherit;
				width: calc(476rpx / 2);
				border-radius: inherit;
				box-shadow: 0rpx 18rpx 72rpx 18rpx rgba(0, 195, 255, 0.1);
				z-index: 1;
				transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
			}
		}

		/* 切换 end */

		/* 登录注册信息 start */
		&__info {
			margin: 0 30rpx;
			margin-top: 105rpx;
			padding: 30rpx 51rpx;
			padding-bottom: 0;
			border-radius: 20rpx;
			background-color: rgba(255, 255, 255, 0.3);
			box-shadow: 0rpx 10rpx 50rpx 0rpx rgba(0, 3, 72, 0.1);

			&__item {

				&__input {
					margin-top: 59rpx;
					width: 100%;
					height: 77rpx;
					border: 1rpx solid #E6E6E6;
					border-radius: 39rpx;

					&__left-icon {
						width: 10%;
						font-size: 44rpx;
						margin-left: 20rpx;
						color: #FFFFFF;
					}

					&__content {
						width: 80%;
						padding-left: 10rpx;

						&--verify-code {
							width: 56%;
						}

						input {
							font-size: 24rpx;
							// letter-spacing: 0.1em;
						}
					}

					&__right-icon {
						width: 10%;
						font-size: 44rpx;
						margin-right: 20rpx;
						color: #AAAAAA;
					}

					&__right-verify-code {
						width: 34%;
						margin-right: 20rpx;
					}
				}

				&__button {
					margin-top: 75rpx;
					margin-bottom: 39rpx;
					width: 100%;
					height: 77rpx;
					text-align: center;
					font-size: 31rpx;
					font-weight: bold;
					line-height: 77rpx;
					letter-spacing: 1em;
					text-indent: 1em;
					border-radius: 39rpx;
					box-shadow: 1rpx 10rpx 24rpx 0rpx rgba(60, 129, 254, 0.35);
				}

				&__tips {
					margin: 30rpx 0;
					color: #f7f7f7;
					// color: #AAAAAA;
				}
			}
		}

		/* 登录注册信息 end */

		/* 登录方式切换 start */
		&__way {
			margin: 0 auto;
			margin-top: 110rpx;

			&__item {
				&--icon {
					width: 77rpx;
					height: 77rpx;
					font-size: 50rpx;
					border-radius: 100rpx;
					margin-bottom: 18rpx;
					position: relative;
					z-index: 1;

					&::after {
						content: " ";
						position: absolute;
						z-index: -1;
						width: 100%;
						height: 100%;
						left: 0;
						bottom: 0;
						border-radius: inherit;
						opacity: 1;
						transform: scale(1, 1);
						background-size: 100% 100%;
						background-image: url(https://tnuiimage.tnkjapp.com/cool_bg_image/icon_bg5.png);
					}
				}
			}
		}

		/* 登录方式切换 end */
		/* 内容 end */

	}

	/deep/.input-placeholder {
		font-size: 24rpx;
		color: #E6E6E6;
	}

	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}
</style>